<?php
class Server {
  public static function get_enquiries($enquiry_id) {
    global $myPdo;
    $cond_vals = new stdClass();
    $cond_vals->c = 'id = :v1';
    $cond_vals->v = array(':v1' => $enquiry_id);
    $opts = new stdClass();
    $opts->select_expr = 't.id, t.date1, t.unread, t.replied_status, t.appointment_date, t.appointment_status, t.date_last_follow, t.date_last_remind, t.date_last_visit';
    $enquiries = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
    
    return $enquiries;
  }
  
  public static function appointment($enquiries) {
  	global $myPdo;
    
    if (count($enquiries) > 0) {
    	$enquiry = $enquiries[0];
    	
    	$cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.appointment_date, t.is_visited';
	    $msgs = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
    
	    $appt_date = '2037-01-01 00:00:00';
	    $status = 0;
	    $is_all_visited = 1;
	    foreach ($msgs as $msg) {
	      if ($msg->appointment_date != '0000-00-00 00:00:00') {
	        $status = 1;
	        if ($msg->is_visited != 1) {
	          $is_all_visited = 0;
	          if (strtotime($msg->appointment_date) < strtotime($appt_date)) {
	            $appt_date = $msg->appointment_date;
	          }
	        }
	      }
	    }
	    
	    $enquiry->appointment_status = $status;
	    $enquiry->appointment_date = $appt_date;
	    
	    if ($enquiry->appointment_status == 1 && $is_all_visited == 1) {
	      $enquiry->appointment_status = 2;
	      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
	    }
    }
  }
  
  //Last Followup
  public static function last_followup($enquiries) {
    global $myPdo;
    
    if (count($enquiries) > 0) {
    	$enquiry = $enquiries[0];
    	
    	$cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.is_draft = :v2 AND t.parent_id = :v2 AND (t.message_type != :v2 AND t.message_type != :v3 AND t.message_type != :v4)';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0, ':v3' => 2, ':v4' => 7);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.date1';
	    $msgs = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
	    
	    
      $enquiry->date_last_follow = (count($msgs) > 0 && $msgs[0]->date1 != '0000-00-00 00:00:00') ? $msgs[0]->date1 : '0000-00-00 00:00:00';
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
    }
  }
  
  //Last Remind
  public static function last_remind($enquiries) {
    global $myPdo;
    
    if (count($enquiries) > 0) {
      $enquiry = $enquiries[0];
      
      $cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.is_draft = :v2 AND t.parent_id = :v2 AND t.reminding_date != :v3';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0, ':v3' => '0000-00-00 00:00:00');
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.reminding_date';
	    $msgs = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
    
    
      $enquiry->date_last_remind = (count($msgs) > 0 && $msgs[0]->reminding_date != '0000-00-00 00:00:00') ? $msgs[0]->reminding_date : '0000-00-00 00:00:00';
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
    }
  }
  
  //Last visited
  public static function last_visit($enquiries) {
    global $myPdo;
    
    if (count($enquiries) > 0) {
      $enquiry = $enquiries[0];
      
      $cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.is_draft = :v2 AND t.parent_id = :v2 AND t.appointment_date != :v3 AND t.is_visited = :v4';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0, ':v3' => '0000-00-00 00:00:00', ':v4' => 1);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.appointment_date';
	    $msgs = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
    
    
      $enquiry->date_last_visit = (count($msgs) > 0 && $msgs[0]->appointment_date != '0000-00-00 00:00:00') ? $msgs[0]->appointment_date : '0000-00-00 00:00:00';
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry); 
    }
  }
  
  
  public static function unread($enquiries) {
  	global $myPdo;
    
    if (count($enquiries) > 0) {
    	$enquiry = $enquiries[0];
    	
    	$cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0);
	    $opts = new stdClass();
	    $opts->select_expr = 'COUNT(t.id) AS total';
	    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
    
      $enquiry->unread = $tmps[0]->total;
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
      
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.enquiry_id = :v1';
	    $cond_vals->v = array(':v1' => $enquiry->id);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.unread';
	    $emails = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
	    if (count($emails) > 0) {
	      foreach ($emails as &$email) {
	        $cond_vals = new stdClass();
	        $cond_vals->c = 't.attr1 = :v1 AND t.parent_id = :v2 AND t.is_draft = 0';
	        $cond_vals->v = array(':v1' => $email->id, ':v2' => 0);
	        $opts = new stdClass();
	        $opts->select_expr = 'COUNT(t.id) AS total';
	        $tmps1 = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
	        $email->unread = $tmps1[0]->total;
	        DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $email);
	      }
	    }
    }
  }
  
  public static function replied_status($enquiries) {
    global $myPdo;
    
    if (count($enquiries) > 0) {
    	$enquiry = $enquiries[0];
    	
    	$cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.is_closed';
      $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
    
    
	    $is_all_closed = 1;
	    foreach ($tmps as &$tmp) {
	      if ($tmp->is_closed == 0) {
	        $is_all_closed = 0;
	      }
	    }
	    $enquiry->replied_status = $is_all_closed;
	    DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
	    
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.enquiry_id = :v1';
	    $cond_vals->v = array(':v1' => $enquiry->id);
	    $opts = new stdClass();
      $opts->select_expr = 't.id, t.replied_status';
	    $emails = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
	    if (count($emails) > 0) {
	      foreach ($emails as &$email) {
	        $email->replied_status = $enquiry->replied_status;
	        DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $email);
	      }
	    }
    }
  }
  
  public static function date1($enquiries) {
  	global $myPdo;
    
    if (count($enquiries) > 0) {
      $enquiry = $enquiries[0];
      
      $cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.date1';
	    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
	    
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.enquiry_id = :v1';
	    $cond_vals->v = array(':v1' => $enquiry->id);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.date1';
	    $emails = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
	    
	    if (count($emails) > 0) {
	      foreach ($emails as &$email) {
	        if (count($tmps) > 0) {
	          if ($tmps[0]->date1 != '0000-00-00 00:00:00') {
	            $email->date1 = $tmps[0]->date1;
	          }
	        }
	        DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $email);
	      }
	    }
    
      if (count($tmps) > 0) {
        if ($tmps[0]->date1 != '0000-00-00 00:00:00') {
          $enquiry->date1 = $tmps[0]->date1;
        }
      } else {
        if (count($emails) > 0) {
          $enquiry->date1 = $emails[0]->date1;
        }
      }
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
    }
  }
  
  public static function is_read($enquiries) {
  	global $myPdo;
  	
  	if (count($enquiries) > 0) {
      $enquiry = $enquiries[0];
      
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.attr2 = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
	    $cond_vals->v = array(':v1' => $enquiry->id, ':v2' => 0);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.is_read';
	    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opts)->d;
	    
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.enquiry_id = :v1';
	    $cond_vals->v = array(':v1' => $enquiry->id);
	    $opts = new stdClass();
	    $opts->select_expr = 't.id, t.is_read';
	    $emails = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
	    
	    $is_readed = 1;
	    foreach ($tmps as &$tmp) {
	      if ($tmp->is_read == 0) {
	        $is_readed = 0;
	      }
	    }
	    if (count($emails) > 0) {
	      foreach ($emails as &$email) {
	        $email->is_read = $is_readed;
	        DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_emails', $email);
	      }
	    }
  	}
  }
  
  public static function calendar($enquiry, $message, $old_messages) {
    Server::addApptEvent($enquiry, $message, $old_messages);
    if ($enquiry->status == 0) {
      Server::addRemindingEvent($enquiry, $message, $old_messages);
    }
  }
  
  public static function addApptEvent($enquiry, $message, $old_messages) {
    global $myPdo;
    if ($message->parent_id == 0 && $message->is_draft == 0) {
    	
      $cond_vals = new stdClass();
      $cond_vals->c = 't.message_id = :v1 AND t.is_deleted = :v2 AND t.type = :v3';
      $cond_vals->v = array(':v1' => $message->id, ':v2' => 0, ':v3' => 1);
      $events = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      
      if (count($events) > 0) {
        $event = $events[0];
        if ($message->appointment_date && $message->appointment_date != '0000-00-00 00:00:00') {
          $event->start = $message->appointment_date;
          $event->end = date('Y-m-d H:i:s', strtotime($message->appointment_date) + 1800);
          $event->title = Server::customerId($enquiry->id) . ' ' . (' (B)' . (($enquiry->b_name != '') ? $enquiry->b_name : '-') . ', ') . ('(G)' . (($enquiry->g_name != '') ? $enquiry->g_name : '-') . ', ' ) . ('(S)' . (($message->appt_user_name != '') ? $message->appt_user_name : '-'));
          $event->description = ($message->appointment_note) ? $message->appointment_note : '';
          $event->is_updated = 1;
          $event->last_updated = date('Y-m-d H:i:s', time());
          $event->is_google_updated = 0;
          DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
        } else {
          $event->is_deleted = 1;
          DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
          
          $myPdo->exec("UPDATE " . TABLE_NAME_PREFIX_MODULE . "data_email_messages SET is_visited = 0 WHERE id = " . $message->id);
        }
      } else {
        if ($message->appointment_date != '0000-00-00 00:00:00') {
          $event = new stdClass();
          $event->track_id = MiscUtils::unique();
          $event->start = $message->appointment_date;
          $event->end = date('Y-m-d H:i:s', strtotime($message->appointment_date) + 1800);
          $event->allDay = 0;
          $event->title = Server::customerId($enquiry->id) . ' ' . (' (B)' . (($enquiry->b_name != '') ? $enquiry->b_name : '-') . ', ') . ('(G)' . (($enquiry->g_name != '') ? $enquiry->g_name : '-') . ', ' ) . ('(S)' . (($message->appt_user_name != '') ? $message->appt_user_name : '-'));
          $event->description = ($message->appointment_note) ? $message->appointment_note : '';
          $event->remind_type = '';
          $event->remind_minute = 0;
          $event->google_id = '';
          $event->is_updated = 0;
          $event->is_deleted = 0;
          $event->type = 1;
          $event->message_id = $message->id;
          $event->last_updated = '0000-00-00 00:00:00';
          $event->is_google_updated = 1;
          DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
        }
      }
      
//      $add = false;
//      if (count($old_messages) > 0) {
//        if ((strtotime($message->appointment_date) == strtotime($old_messages[0]->appointment_date)) && $message->appt_user_id == $old_messages[0]->appt_user_id) {
//          $add = true;
//        }
//      }
//      
//      if (!$add) {
//        if (isset($event)) {
//          Server::add_home_message($event, $message, $old_messages);
//        }
//      }
    } else if ($message->parent_id == 0 && $message->is_draft == 1) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.message_id = :v1 AND t.is_deleted = :v2 AND t.type = :v3';
      $cond_vals->v = array(':v1' => $message->id, ':v2' => 0, ':v3' => 1);
      $events = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      if (count($events) > 0) {
        $event = $events[0];
        $event->is_deleted = 1;
        DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
      }
    }
  }
  
  public static function addRemindingEvent($enquiry, $message, $old_messages) {
    global $myPdo;
    if ($message->parent_id == 0 && $message->is_draft == 0) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.message_id = :v1 AND t.is_deleted = :v2 AND t.type = :v3';
      $cond_vals->v = array(':v1' => $message->id, ':v2' => 0, ':v3' => 2);
      $events = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      if (count($events) > 0) {
        $event = $events[0];
        if ($message->reminding_type != 1 && $message->reminding_date && $message->reminding_date != '0000-00-00 00:00:00') {
          $event->start = $message->reminding_date;
          $event->end = date('Y-m-d H:i:s', strtotime($message->reminding_date) + 1800);
          $event->title = Server::customerId($enquiry->id) . ' ' . (' (B)' . (($enquiry->b_name != '') ? $enquiry->b_name : '-') . ', ') . ('(G)' . (($enquiry->g_name != '') ? $enquiry->g_name : '-'));
          $event->description = ($message->reminding_note) ? $message->reminding_note : '';
          $event->is_updated = 1;
          $event->last_updated = date('Y-m-d H:i:s', time());
          $event->is_google_updated = 0;
          DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
        } else {
          $event->is_deleted = 1;
          DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
          
          $myPdo->exec("UPDATE " . TABLE_NAME_PREFIX_MODULE . "data_email_messages SET is_reminding_closed = 0, reminding_closed_date = '0000-00-00 00:00:00' WHERE id = " . $message->id);
        }
      } else {
        if ($message->reminding_date != '0000-00-00 00:00:00') {
          $event = new stdClass();
          $event->track_id = MiscUtils::unique();
          $event->start = $message->reminding_date;
          $event->end = date('Y-m-d H:i:s', strtotime($message->reminding_date) + 1800);
          $event->allDay = 0;
          $event->title = Server::customerId($enquiry->id) . ' ' . (' (B)' . (($enquiry->b_name != '') ? $enquiry->b_name : '-') . ', ') . ('(G)' . (($enquiry->g_name != '') ? $enquiry->g_name : '-'));
          $event->description = ($message->reminding_note) ? $message->reminding_note : '';
          $event->remind_type = '';
          $event->remind_minute = 0;
          $event->google_id = '';
          $event->is_updated = 0;
          $event->is_deleted = 0;
          $event->type = 2;
          $event->message_id = $message->id;
          $event->last_updated = '0000-00-00 00:00:00';
          $event->is_google_updated = 1;
          DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
        }
      }
    } else if ($message->parent_id == 0 && $message->is_draft == 1) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.message_id = :v1 AND t.is_deleted = :v2 AND t.type = :v3';
      $cond_vals->v = array(':v1' => $message->id, ':v2' => 0, ':v3' => 2);
      $events = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      if (count($events) > 0) {
        $event = $events[0];
        $event->is_deleted = 1;
        DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
      }
    }
  }
  
  public static function changeMsgAppt($event, $mode) {
    global $myPdo;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
    $cond_vals->v = array(':v1' => $event->message_id, ':v2' => 0);
    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, NULL)->d;
    
    if (count($tmps) > 0) {
      $msg = $tmps[0];
      if ($mode == 1) {
      	$msg->appointment_date = '0000-00-00 00:00:00';
      	$msg->appt_user_id = 0;
      	$msg->appt_user_name = '';
      	$msg->appointment_note = '';
      	$msg->is_visited = 0;
      } else {
        $msg->appointment_date = $event->start;
        $msg->appointment_note = $event->description;
      }
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $msg);
      
      $enqs = Server::get_enquiries($tmp->attr2);
      Server::appointment($enqs);
      Server::last_visit($enqs);
    }
  }
  
  public static function changeMsgRmd($event, $mode) {
    global $myPdo;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't1.status = 0 AND t.id = :v1 AND t.parent_id = :v2 AND t.is_draft = :v2';
    $cond_vals->v = array(':v1' => $event->message_id, ':v2' => 0);
    $opt = new stdClass();
    $opt->select_expr = 't.*';
    $opt->join_expr = 'LEFT JOIN ' . TABLE_NAME_PREFIX_MODULE . 'data_enquiry AS t1 ON t.attr2 = t1.id';
    $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $cond_vals, 't.date1', 'DESC', NULL, NULL, $opt)->d;
    
    if (count($tmps) > 0) {
      $msg = $tmps[0];
      
      if ($mode == 1) {
        $msg->reminding_date = '0000-00-00 00:00:00';
        $msg->reminding_type = 1;
        $msg->reminding_days = 0;
        $msg->reminding_note = '';
        $msg->is_reminding_closed = 0;
        $msg->reminding_closed_date = '0000-00-00 00:00:00';
      } else {
        $msg->reminding_date = $event->start;
        if ($msg->reminding_type == 2) {
          $msg->reminding_days = (strtotime(date('Y-m-d', strtotime($msg->reminding_date))) - strtotime(date('Y-m-d', time()))) > 0 ? ((strtotime(date('Y-m-d', strtotime($msg->reminding_date))) - strtotime(date('Y-m-d', time()))) / 86400) + 1 : 0;
        }
        $msg->reminding_note = $event->description;
      }
      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $msg);
      $enqs = Server::get_enquiries($msg->attr2);
      Server::last_remind($enqs);
    }
  }
  
  public static function add_home_message($event, $message, $old_messages) {
    global $myPdo;
    
    $target_id = count($old_messages) > 0 ? $old_messages[0]->appt_user_id : 0;
    $target_name = count($old_messages) > 0 ? $old_messages[0]->appt_user_name : '';
        
    $obj = MiscUtils::pluginObj();
    $obj->date1 = date('Y-m-d H:i:s', time());
    $obj->date2 = '0000-00-00 00:00:00';
    $obj->date3 = '0000-00-00 00:00:00';
    $obj->attr1 = (isset($_SESSION[CLIENT])) ? $_SESSION[CLIENT]->id : 0;
    $obj->attr2 = ($message->appt_user_id != 0) ? $message->appt_user_id : $target_id;
    $obj->attr3 = ($message->appointment_date && $message->appointment_date != '0000-00-00 00:00:00') ? $event->title . ', ' . substr($message->appointment_date, 0, 16) : '[Cancelled] ' . $event->title;
    $obj->attr4 = 0;
    $obj->attr5 = 0;
    $obj->attr6 = (isset($_SESSION[CLIENT])) ? $_SESSION[CLIENT]->attr3 : '';
    $obj->attr7 = ($message->appt_user_id != 0) ? $message->appt_user_name : $target_name;
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_messages', $obj);
  }
  
  public static function is_visited($message) {
    global $myPdo;
    
    return;
    $cond_vals = new stdClass();
    $cond_vals->c = 'id = :v1';
    $cond_vals->v = array(':v1' => $message->attr2);
    $opts = new stdClass();
    $opts->select_expr = 't.id, t.b_name, t.g_name';
    $enquiries = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
    if (count($enquiries) > 0) {
    	$enquiry = $enquiries[0];
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.message_id = :v1 AND t.is_deleted = :v2 AND t.type = :v3';
      $cond_vals->v = array(':v1' => $message->id, ':v2' => 0, ':v3' => 1);
      $opts = new stdClass();
      $opts->select_expr = 't.id, t.title';
	    $events = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
	    if (count($events) > 0) {
	      $event = $events[0];
	      $event->title = (($message->is_visited ==1) ? '(V)' : '') . 'Appt: ' . Server::customerId($enquiry->id) . ', ' . ((($enquiry->b_name != '') ? $enquiry->b_name : '-') . ' (Bride), ' ) . ((($enquiry->g_name != '') ? $enquiry->g_name : '-') . ' (Groom), ' ) . ((($message->appt_user_name != '') ? $message->appt_user_name : '-') . ' (Staff)');
	      DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
	    }
    }
  }
  
  public static function is_completed($message) {
    global $myPdo;
    return;
    $cond_vals = new stdClass();
    $cond_vals->c = 'id = :v1';
    $cond_vals->v = array(':v1' => $message->attr2);
    $opts = new stdClass();
    $opts->select_expr = 't.id, t.b_name, t.g_name';
    $enquiries = DbUtils::get($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
    if (count($enquiries) > 0) {
      $enquiry = $enquiries[0];
      $cond_vals = new stdClass();
      $cond_vals->c = 't.message_id = :v1 AND t.is_deleted = :v2 AND t.type = :v3';
      $cond_vals->v = array(':v1' => $message->id, ':v2' => 0, ':v3' => 2);
      $opts = new stdClass();
      $opts->select_expr = 't.id, t.title';
      $events = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d;
      if (count($events) > 0) {
        $event = $events[0];
        $event->title = (($message->is_urgent == 1) ? '(Urgent)' : '') . (($message->is_reminding_closed ==1) ? '(C)' : '') . 'Rmd: ' . Server::customerId($enquiry->id) . ', ' . ((($enquiry->b_name != '') ? $enquiry->b_name : '-') . ' (Bride), ' ) . ((($enquiry->g_name != '') ? $enquiry->g_name : '-') . ' (Groom)' );
        DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'calendar_event', $event);
      }
    }
  }
  
  public static function customerId($id) {
    if ($id < 10000 && $id >= 1000) {
      $id = $id;
    } else if ($id < 1000 && $id >= 100) {
      $id = '0' . $id;
    } if ($id < 100 && $id >= 10) {
      $id = '00' . $id;
    } if ($id < 10 && $id > 0) {
      $id = '000' . $id;
    }
    return $id;
  }
}
?>